<?php
namespace app\system\controller;

use think\Request;

class RouteController extends BaseController
{

    /**
     * 初始化
     * AreaController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $path = APP_PATH . 'route.php';
        if ($this->request->isAjax()) {
            $item = $this->request->param('item/a');
            $code = '<?php' . PHP_EOL;
            $code .= 'return [' . PHP_EOL;
            if (!empty($item)) {
                foreach ($item as $v) {
                    $code .= "'{$v['item_key']}'=>'{$v['item_value']}'," . PHP_EOL;
                }
            }
            $code .= '];' . PHP_EOL;
            $result = file_put_contents($path, $code);
            if ($result === false) {
                $this->error('服务器目录不可写');
            } else {
                $this->success('生成成功！', url('index'));
            }
            exit;
        }
        $route = require $path;
        $arr   = [];
        foreach ($route as $key => $value) {
            $arr[] = ['item_key' => $key, 'item_value' => $value];
        }
        $this->assign('route', json_encode($arr, true));
        return $this->fetch();
    }

}
